<?php
namespace app\admin2\controller;

use app\common\controller\AdminBaseo;
use app\common\model\Timedtask as TimedtaskModel;
use think\Session;

class Timedtask extends AdminBaseo
{

    protected $timedtask_model;
    protected $config;

    protected function _initialize(){
        parent::_initialize();
        $this->config = Session::get('wechat_info'); //当前授权公众号信息
        $this->timedtask_model = new TimedtaskModel;
    }

    /**
     * 保存定时任务
     */
    public function saveTimedTask() {
        if($this->request->isPost()){
            $data = $this->request->post();

            $validate_result = $this->validate('Timetask' , $data);
            if($validate_result !== true){
                return json(['status' => -1 , 'error' => $validate_result]);
            }else{
                    $arr_data = [
                        'type' => 'time_after_test',
                        'data' => [
                            'timer' => $data['appoint_time'],
                            'content' => $data['content'],
                            'appoint_type' => $data['appoint_type'],
                            'appoint_user' => $data['appoint_user'],
                            'id' => $this->config['id'],
                            // 'uid' => $this->config['uid'],
                        ],
                    ];
                    // 发送websocket 信息
                    $socket_result = sendWebSocket($arr_data);
                    if ($socket_result) {
                        $data['auid'] = $this->config['id'];
                        $result = $this->timedtask_model->allowField(true)->save($data);
                        if($result){
                            return json(['status' => 1 , 'msg' => 'ok']);
                        } else {
                            return json(['status' => -1 , 'error' => 'save timedtask failed']);
                        }
                    } else {
                        return json(['status' => -1 , 'error' => 'creating timedtask failed']);
                    }
            }
        }        
    }

    // public function test() {
    //     $arr_data = [
    //         'type' => 'time_after_test',
    //         'data' => [
    //             'timer' => 1111,
    //             'content' => 1111,
    //             'appoint_type' => 1111,
    //             'appoint_user' => 1111,
    //             'id' => 1111,
    //             // 'uid' => $this->config['uid'],
    //         ],
    //     ];
    //     // 发送websocket 信息
    //     $socket_result = sendWebSocket($arr_data);
    //     dump($socket_result);
    // }
}